package orm

import (
	"database/sql"
	"github.com/jinzhu/gorm"
	"time"
)

type User struct {
	gorm.Model
	Birthday time.Time
	Age      int
	Name     string `gorm:"size:255"`
	Num      int    `gorm:"AUTO_INCREAMENT"`

	CreditCard CreditCard
	Email      []Email

	BillingAddress   Address
	BillingAddressID sql.NullInt64

	ShippingAddress   Address
	ShippingAddressID int

	IgnoreMe  int        `gorm:"-"`
	Languages []Language `gorm:"many2many:user_languages"`
}

type Email struct {
	ID         int
	UserID     int    `gorm:"index"`
	Email      string `gorm:"type:varchar(100);unique_index"`
	Subscribed bool
}

type Address struct {
	ID       int
	Address1 string         `gorm:"not null;unique"` // 设置字段为非空并唯一
	Address2 string         `gorm:"type:varchar(100);unique"`
	Post     sql.NullString `gorm:"not null"`
}

type Language struct {
	ID   int    `gorm:"primary_key;AUTO_INCREMENT"`
	Name string `gorm:"index:idx_name_code"`
	Code string `gorm:"index:idx_name_code"`
}

type CreditCard struct {
	gorm.Model
	UserID int
	Number string
}

func InsertLanguage(languager []*Language, db *gorm.DB) error {
	db.Create(languager)
	return db.Error
}
